<?php
/**
 * ClassName: card_log
 * Description: 会员卡消费/充值记录
 * @Author 周昌
 * @Create 2023/11/17 9:13
 */

namespace app;


use epii\server\Args;
use HPlus\Validate\Validate;
use libs\Tool;
use model\CardModel;
use model\MemberModel;
use service\admin\CardCateService;
use service\admin\CardPreferentialService;
use service\admin\CardLogService;
use service\admin\HospitalService;
use service\admin\RoleService;

class card_log extends base
{
    protected $admin_allow=['index','ajax_data','income','recharge'];
    public function init()
    {
        parent::init();
        $this->accessDenied();
    }
    public function index()
    {
        $card_id=Args::params('card_id/d');
        if($this->adminInfo['role']==1){
            $this->assign('hospitals',HospitalService::getSelect());
        }
        //筛选
        $this->_as_type=CardLogService::cardLogTypeSelect();
        $this->assign('card_id',$card_id);
        $this->adminUiDisplay();
    }

    public function ajax_data()
    {
        $map = [];
        //登录用户权限判断
        $hospital_id=$this->adminInfo['hospital_id'];
        if($hospital_id){
            $map[]=['hospital_id','=',$hospital_id];
        }
        $card_id=Args::params('card_id/d');
        if($card_id){
            $map[]=['card_id','=',$card_id];
        }
        $type=Args::params('type/d');
        if(is_int($type)){
            $map[]=['type','=',$type];
        }
        echo CardLogService::getAjaxData($map);
    }


    /**
     * @return array|false|string* 删除
     */
    public function del()
    {
        $id = Args::params('id');
        //todo 判断分类下是否已经有，无则可以删除
        $res = CardLogService::del($id);
        if ($res['code']) {
            $this->success($res['msg'],'refresh');
        } else {
            $this->error($res['msg']);
        }
    }
    //真实收入统计列表
    public function income(){

        if(Args::isPost()){
            $map = [];
            //登录用户权限判断
            $hospital_id=$this->adminInfo['hospital_id'];
            if($hospital_id){
                $map[]=['hospital_id','=',$hospital_id];
            }
            //时间筛选
            $start_time=Args::params('start_time');
            if($start_time){
                $start_time=strtotime($start_time);
                $map[]=['addtime','>=',$start_time];
            }
            $end_time=Args::params('end_time');
            if($end_time){
                $end_time=strtotime($end_time);
                $map[]=['addtime','<=',$end_time];
            }
            echo CardLogService::incomData($map);
            die;

        }
        if($this->adminInfo['role']==1){
            $this->assign('hospitals',HospitalService::getSelect());
        }
        $this->adminUiDisplay();

    }
    //充值统计
    public function recharge(){
        if(Args::isPost()){
            $map = [];
            //登录用户权限判断
            $hospital_id=$this->adminInfo['hospital_id'];
            if($hospital_id){
                $map[]=['hospital_id','=',$hospital_id];
            }
            //时间筛选
            $start_time=Args::params('start_time');
            if($start_time){
                $start_time=strtotime($start_time);
                $map[]=['addtime','>=',$start_time];
            }
            $end_time=Args::params('end_time');
            if($end_time){
                $end_time=strtotime($end_time);
                $map[]=['addtime','<=',$end_time];
            }
            $map[]=['type','=',CardLogService::$card_log_type_index['recharge']];
            echo CardLogService::rechargeData($map);
            die;

        }
        if($this->adminInfo['role']==RoleService::$role['admin']['id']){
            $this->assign('hospitals',HospitalService::getSelect());
        }
        $this->adminUiDisplay();
    }

}